package com.gopay.cashier.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gopay.cashier.service.PersonUsedQuotaService;
import com.gopay.common.constants.trans.CustCategory;
import com.gopay.common.constants.trans.CustChannelNum;
import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.domain.user.identify.PersIdUsedQuota;
import com.gopay.common.domain.user.identify.RcAcctPayLimit;
import com.gopay.common.domain.user.identify.RcPayVerifyLimit;
import com.gopay.common.domain.user.identify.vo.PersonTypeQuotaVo;
import com.gopay.common.user.dao.PersCertChannelDtlDao;
import com.gopay.common.user.dao.PersonIdentifyDAO;
import com.gopay.common.user.dao.RcAcctPayLimitDao;
import com.gopay.common.user.dao.RcPayVerifyLimitDAO;
import com.gopay.common.user.manager.CustPersonInfoQueryManager;

@Service("personIdUsedQuotaService")
public class PersonUsedQuotaServiceImpl implements PersonUsedQuotaService {

    @Autowired
    private CustPersonInfoQueryManager custPersonInfoQueryManager;
    
    @Autowired
    private PersonIdentifyDAO personIdentifyDAO;
    
    @Autowired
    private RcAcctPayLimitDao rcAcctPayLimitDao;
    
    @Autowired 
    private PersCertChannelDtlDao persCertChannelDtlDao;
    
    @Autowired
    private RcPayVerifyLimitDAO rcPayVerifyLimitDAO;
    
    @Override
    public PersonTypeQuotaVo findPersonTypeQuota(String custId) {
        PersonTypeQuotaVo result = null;
        
        //1.根据个人客户ID查询   --> 个人客户信息（证件号）
        CustPersonInfo custInfo = custPersonInfoQueryManager.getByCustId(custId);
        if(custInfo == null){
            return result;
        }
        
        //2.根据证件号查询   --> 个人用户  （限额）
        PersIdUsedQuota personUsedQuota = personIdentifyDAO.findByIndentifyNo(custInfo.getCertNo());
        if(personUsedQuota == null){
            return result;
        }
        
        //3.查询客户渠道通过个数,并判定   （类别）
        int count = persCertChannelDtlDao.findSuccessCertChannelCountsByCustId(custInfo.getCustId());
        
        
        //4.根据用户类别查询  --> 系统限额配置
        RcAcctPayLimit acctPayLimit = rcAcctPayLimitDao.findByAcctType(CustChannelNum.custType(count));
        if(acctPayLimit == null){
            return result;
        }
        
        //5.计算个人账户 类别, 已用限额, 可用限额
        result = new PersonTypeQuotaVo();
        
//        String type = personUsedQuota.getCustCategory();  //弃用(原先绑定身份证，现在绑定客户ID)
        String type = CustChannelNum.custType(count);
        result.setType(type);       //类别
        
        BigDecimal  usedQuota = null;//已用
        if(CustCategory.one.toString().equals(type)){
            //一类账户
            usedQuota = personUsedQuota.getAllUsedQuota();
        }else{
            //二三类账户
            usedQuota = personUsedQuota.getYearUsedQuota();
        }
        //已用额度
        result.setUsedLimit(usedQuota.toPlainString()); 
        
        BigDecimal  canUsed = acctPayLimit.getAcctLimit().subtract(usedQuota);//限用
        if(canUsed.doubleValue() <= 0.0){
            result.setCanUseLimit("0");
        }else{
            result.setCanUseLimit(canUsed.toPlainString());
        }
        
        return result;
    }

	@Override
	public String[] findDailyUsedQuata(String custId, String payType) {
		//单日已用额度
		BigDecimal dailyUsedQuota = personIdentifyDAO.findMainorderAmt(custId);
		//支付验证方式配置
		RcPayVerifyLimit rcPayVerifyLimit = rcPayVerifyLimitDAO.findByCode(payType);
		//单日限额
		BigDecimal dailyLimit = rcPayVerifyLimit.getTodayTotalLimit();
		//单日可用
		BigDecimal dailyCanUse = dailyLimit.subtract(dailyUsedQuota);
        String dailyCanUseStr = (dailyCanUse.doubleValue()) <= 0.0 ? "0" : dailyCanUse.toPlainString();
		
		return new String[]{dailyLimit.toPlainString(), dailyCanUseStr};
	}

}
